﻿using AnesSystem.Models;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem
{
    public class DrugsDataCache
    {
        #region Variables

        private Dictionary<string, string> mapExeIDToHashCode = new Dictionary<string, string>();

        private Dictionary<string, List<PatientDrugsModel>> _anesMedicineMapCodeList = new Dictionary<string, List<PatientDrugsModel>>();

        private Dictionary<string, List<PatientDrugsModel>> _anesInfusionMapCodeList = new Dictionary<string, List<PatientDrugsModel>>();

        private ObservableCollection<PatientDrugsModel> _anesSpecialDrugList = new ObservableCollection<PatientDrugsModel>();

        private Dictionary<string, List<PatientDrugsModel>> _pacuMedicineMapCodeList = new Dictionary<string, List<PatientDrugsModel>>();

        private Dictionary<string, List<PatientDrugsModel>> _pacuInfusionMapCodeList = new Dictionary<string, List<PatientDrugsModel>>();

        private ObservableCollection<PatientDrugsModel> _pacuSpecialDrugList = new ObservableCollection<PatientDrugsModel>();

        private ObservableCollection<PatientDrugsModel> _allPatientDrugsList = new ObservableCollection<PatientDrugsModel>();

        private PatientLinkModel _patientLink;
        #endregion

        #region Constructor
        public DrugsDataCache(PatientLinkModel p)
        {
            this._patientLink = p;
        }
        #endregion

        #region Properties
        public Dictionary<string, List<PatientDrugsModel>> AnesMedicineMapCodeList
        {
            get
            {
                return _anesMedicineMapCodeList;
            }

            set
            {
                _anesMedicineMapCodeList = value;
            }
        }

        public ObservableCollection<PatientDrugsModel> AnesSpecialDrugList
        {
            get
            {
                return _anesSpecialDrugList;
            }

            set
            {
                _anesSpecialDrugList = value;
            }
        }

        public Dictionary<string, string> MapExeIDToHashCode
        {
            get
            {
                return mapExeIDToHashCode;
            }

            set
            {
                mapExeIDToHashCode = value;
            }
        }

        public Dictionary<string, List<PatientDrugsModel>> AnesInfusionMapCodeList
        {
            get
            {
                return _anesInfusionMapCodeList;
            }

            set
            {
                _anesInfusionMapCodeList = value;
            }
        }

        public ObservableCollection<PatientDrugsModel> AllPatientDrugsList
        {
            get
            {
                return _allPatientDrugsList;
            }

            set
            {
                _allPatientDrugsList = value;
            }
        }

        public Dictionary<string, List<PatientDrugsModel>> PacuMedicineMapCodeList
        {
            get
            {
                return _pacuMedicineMapCodeList;
            }

            set
            {
                _pacuMedicineMapCodeList = value;
            }
        }

        public Dictionary<string, List<PatientDrugsModel>> PacuInfusionMapCodeList
        {
            get
            {
                return _pacuInfusionMapCodeList;
            }

            set
            {
                _pacuInfusionMapCodeList = value;
            }
        }

        public ObservableCollection<PatientDrugsModel> PacuSpecialDrugList
        {
            get
            {
                return _pacuSpecialDrugList;
            }

            set
            {
                _pacuSpecialDrugList = value;
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        public void DbSearch()
        {
            ClearCache();
            //DateTime startTime = _patientLink.Status.InRoomTime;
            //DateTime endTime;
            //if (_patientLink.Status.OutRoomTime != new DateTime())
            //{
            //    endTime = _patientLink.Status.OutRoomTime;
            //}
            //else
            //{
            //    endTime =DateTime.Now;
            //}

            foreach (var item in PatientDrugsService.Instance.GetPatientDrugs8Time(_patientLink.Id.ToString()))
            {
                AllPatientDrugsList.Add(item);
                addData(item);
            }
        }

        public void ClearCache()
        {
            AnesMedicineMapCodeList.Clear();
            AnesInfusionMapCodeList.Clear();

            PacuInfusionMapCodeList.Clear();
            PacuMedicineMapCodeList.Clear();
            
            AnesSpecialDrugList.Clear();
            PacuSpecialDrugList.Clear();

            AllPatientDrugsList.Clear();
            mapExeIDToHashCode.Clear();
        }

        public void addData(PatientDrugsModel exe)
        {

            try
            {
                if (exe != null && exe.Type != null)
                {
                    if (exe.Type.Equals(Constants.DRUG_SPECIAL))
                    {
                        AddAnesSpecialDrug(exe);
                    }
                    else if (exe.Type.Equals(Constants.DRUG_SHUYE))
                    {
                        AddAnesInfusionDrug(exe);
                    }
                    else if (exe.Type.Equals(Constants.DRUG_SHUZHONG))
                    {
                        AddAnesMedicineDrug(exe);
                    }
                    else if (exe.Type.Equals(Constants.DRUG_FUSU_SPECIAL))
                    {
                        AddPacuSpecialDrug(exe);
                    }
                    else if (exe.Type.Equals(Constants.DRUG_FUSU_SHUYE))
                    {
                        AddPacuInfusionDrug(exe);
                    }
                    else if (exe.Type.Equals(Constants.DRUG_FUSU_SHUZHONG))
                    {
                        AddPacuMedicineDrug(exe);
                    }
                }
            }
            catch (Exception)
            {

            }

        }

        private void AddAnesMedicineDrug(PatientDrugsModel exe)
        {
            //drug 改变
            if (!mapExeIDToHashCode.ContainsKey(exe.Id.ToString()))
            {
                //新增exe
                MapExeIDToHashCode.Add(exe.Id.ToString(), exe.Drugs.HashCode);
                List<PatientDrugsModel> PatientDrugss = null;
                if (!AnesMedicineMapCodeList.ContainsKey(exe.Drugs.HashCode))
                {
                    PatientDrugss = new List<PatientDrugsModel>();
                    PatientDrugss.Add(exe);
                    AnesMedicineMapCodeList.Add(exe.Drugs.HashCode, PatientDrugss);
                }
                else
                {
                    PatientDrugss = AnesMedicineMapCodeList[exe.Drugs.HashCode];
                    PatientDrugss.Add(exe);
                    AnesMedicineMapCodeList[exe.Drugs.HashCode] = PatientDrugss;
                }
            }
            else
            {
                string oldHashCode = mapExeIDToHashCode[exe.Id.ToString()];
                List<PatientDrugsModel> PatientDrugss = AnesMedicineMapCodeList[oldHashCode];
                PatientDrugsModel pDrug = PatientDrugss.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                if ("invalid".Equals(exe.Status))
                {
                    PatientDrugss.Remove(pDrug);
                    if (PatientDrugss.Count < 1)
                    {
                        MapExeIDToHashCode.Remove(exe.Id.ToString());
                        AnesMedicineMapCodeList.Remove(exe.Drugs.HashCode);
                    }
                    return;
                }
                //修改exe                
                if (oldHashCode.Equals(exe.Drugs.HashCode))
                {  //药配置不变
                    PatientDrugss.Remove(pDrug);
                    PatientDrugss.Add(exe);
                    AnesMedicineMapCodeList[oldHashCode] = PatientDrugss;
                }
                else
                {
                    //改变了Drug 配置
                    mapExeIDToHashCode[exe.Id.ToString()] = exe.Drugs.HashCode; //覆盖之前的
                    List<PatientDrugsModel> PatientDrugs = AnesMedicineMapCodeList[oldHashCode];
                    PatientDrugsModel pDrugs = PatientDrugs.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                    PatientDrugs.Remove(pDrugs);  //清除旧的
                    if (PatientDrugs.Count() < 1) AnesMedicineMapCodeList.Remove(oldHashCode);

                    List<PatientDrugsModel> PatientDrugsList = null;
                    if (!AnesMedicineMapCodeList.ContainsKey(exe.Drugs.HashCode))
                    {
                        PatientDrugsList = new List<PatientDrugsModel>();
                        PatientDrugsList.Add(exe);
                        AnesMedicineMapCodeList.Add(exe.Drugs.HashCode, PatientDrugsList);
                    }
                    else
                    {
                        PatientDrugsList = AnesMedicineMapCodeList[exe.Drugs.HashCode];
                        PatientDrugsList.Add(exe);
                        AnesMedicineMapCodeList[exe.Drugs.HashCode] = PatientDrugsList;
                    }
                }
            }
        }

        private void AddPacuMedicineDrug(PatientDrugsModel exe)
        {
            //drug 改变
            if (!mapExeIDToHashCode.ContainsKey(exe.Id.ToString()))
            {
                //新增exe
                MapExeIDToHashCode.Add(exe.Id.ToString(), exe.Drugs.HashCode);
                List<PatientDrugsModel> PatientDrugss = null;
                if (!PacuMedicineMapCodeList.ContainsKey(exe.Drugs.HashCode))
                {
                    PatientDrugss = new List<PatientDrugsModel>();
                    PatientDrugss.Add(exe);
                    PacuMedicineMapCodeList.Add(exe.Drugs.HashCode, PatientDrugss);
                }
                else
                {
                    PatientDrugss = PacuMedicineMapCodeList[exe.Drugs.HashCode];
                    PatientDrugss.Add(exe);
                    PacuMedicineMapCodeList[exe.Drugs.HashCode] = PatientDrugss;
                }
            }
            else
            {
                string oldHashCode = mapExeIDToHashCode[exe.Id.ToString()];
                List<PatientDrugsModel> PatientDrugss = PacuMedicineMapCodeList[oldHashCode];
                PatientDrugsModel pDrug = PatientDrugss.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                if ("invalid".Equals(exe.Status))
                {
                    PatientDrugss.Remove(pDrug);
                    if (PatientDrugss.Count < 1)
                    {
                        MapExeIDToHashCode.Remove(exe.Id.ToString());
                        PacuMedicineMapCodeList.Remove(exe.Drugs.HashCode);
                    }
                    return;
                }
                //修改exe                
                if (oldHashCode.Equals(exe.Drugs.HashCode))
                {  //药配置不变
                    PatientDrugss.Remove(pDrug);
                    PatientDrugss.Add(exe);
                    PacuMedicineMapCodeList[oldHashCode] = PatientDrugss;
                }
                else
                {
                    //改变了Drug 配置
                    mapExeIDToHashCode[exe.Id.ToString()] = exe.Drugs.HashCode; //覆盖之前的
                    List<PatientDrugsModel> PatientDrugs = PacuMedicineMapCodeList[oldHashCode];
                    PatientDrugsModel pDrugs = PatientDrugs.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                    PatientDrugs.Remove(pDrugs);  //清除旧的
                    if (PatientDrugs.Count() < 1) PacuMedicineMapCodeList.Remove(oldHashCode);

                    List<PatientDrugsModel> PatientDrugsList = null;
                    if (!PacuMedicineMapCodeList.ContainsKey(exe.Drugs.HashCode))
                    {
                        PatientDrugsList = new List<PatientDrugsModel>();
                        PatientDrugsList.Add(exe);
                        PacuMedicineMapCodeList.Add(exe.Drugs.HashCode, PatientDrugsList);
                    }
                    else
                    {
                        PatientDrugsList = PacuMedicineMapCodeList[exe.Drugs.HashCode];
                        PatientDrugsList.Add(exe);
                        PacuMedicineMapCodeList[exe.Drugs.HashCode] = PatientDrugsList;
                    }
                }
            }
        }
        private void AddAnesInfusionDrug(PatientDrugsModel exe)
        {
            //drug 改变
            if (!mapExeIDToHashCode.ContainsKey(exe.Id.ToString()))
            {
                //新增exe
                MapExeIDToHashCode.Add(exe.Id.ToString(), exe.Drugs.HashCode);
                List<PatientDrugsModel> PatientDrugss = null;
                if (!AnesInfusionMapCodeList.ContainsKey(exe.Drugs.HashCode))
                {
                    PatientDrugss = new List<PatientDrugsModel>();
                    PatientDrugss.Add(exe);
                    AnesInfusionMapCodeList.Add(exe.Drugs.HashCode, PatientDrugss);
                }
                else
                {
                    PatientDrugss = AnesInfusionMapCodeList[exe.Drugs.HashCode];
                    PatientDrugss.Add(exe);
                    AnesInfusionMapCodeList[exe.Drugs.HashCode] = PatientDrugss;
                }
            }
            else
            {
                string oldHashCode = mapExeIDToHashCode[exe.Id.ToString()];
                List<PatientDrugsModel> PatientDrugss = AnesInfusionMapCodeList[oldHashCode];
                PatientDrugsModel pDrug = PatientDrugss.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                if ("invalid".Equals(exe.Status))
                {
                    PatientDrugss.Remove(pDrug);
                    if (PatientDrugss.Count < 1)
                    {
                        MapExeIDToHashCode.Remove(exe.Id.ToString());
                        AnesInfusionMapCodeList.Remove(exe.Drugs.HashCode);
                    }
                    return;
                }
                //修改exe                
                if (oldHashCode.Equals(exe.Drugs.HashCode))
                {  //药配置不变
                    PatientDrugss.Remove(pDrug);
                    PatientDrugss.Add(exe);
                    AnesInfusionMapCodeList[oldHashCode] = PatientDrugss;
                }
                else
                {
                    //改变了Drug 配置
                    mapExeIDToHashCode[exe.Id.ToString()] = exe.Drugs.HashCode; //覆盖之前的
                    List<PatientDrugsModel> PatientDrugs = AnesInfusionMapCodeList[oldHashCode];
                    PatientDrugsModel pDrugs = PatientDrugs.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                    PatientDrugs.Remove(pDrugs);  //清除旧的
                    if (PatientDrugs.Count() < 1) AnesInfusionMapCodeList.Remove(oldHashCode);

                    List<PatientDrugsModel> PatientDrugsList = null;
                    if (!AnesInfusionMapCodeList.ContainsKey(exe.Drugs.HashCode))
                    {
                        PatientDrugsList = new List<PatientDrugsModel>();
                        PatientDrugsList.Add(exe);
                        AnesInfusionMapCodeList.Add(exe.Drugs.HashCode, PatientDrugsList);
                    }
                    else
                    {
                        PatientDrugsList = AnesInfusionMapCodeList[exe.Drugs.HashCode];
                        PatientDrugsList.Add(exe);
                        AnesInfusionMapCodeList[exe.Drugs.HashCode] = PatientDrugsList;
                    }
                }
            }
        }
        private void AddPacuInfusionDrug(PatientDrugsModel exe)
        {
            //drug 改变
            if (!mapExeIDToHashCode.ContainsKey(exe.Id.ToString()))
            {
                //新增exe
                MapExeIDToHashCode.Add(exe.Id.ToString(), exe.Drugs.HashCode);
                List<PatientDrugsModel> PatientDrugss = null;
                if (!PacuInfusionMapCodeList.ContainsKey(exe.Drugs.HashCode))
                {
                    PatientDrugss = new List<PatientDrugsModel>();
                    PatientDrugss.Add(exe);
                    PacuInfusionMapCodeList.Add(exe.Drugs.HashCode, PatientDrugss);
                }
                else
                {
                    PatientDrugss = PacuInfusionMapCodeList[exe.Drugs.HashCode];
                    PatientDrugss.Add(exe);
                    PacuInfusionMapCodeList[exe.Drugs.HashCode] = PatientDrugss;
                }
            }
            else
            {
                string oldHashCode = mapExeIDToHashCode[exe.Id.ToString()];
                List<PatientDrugsModel> PatientDrugss = PacuInfusionMapCodeList[oldHashCode];
                PatientDrugsModel pDrug = PatientDrugss.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                if ("invalid".Equals(exe.Status))
                {
                    PatientDrugss.Remove(pDrug);
                    if (PatientDrugss.Count < 1)
                    {
                        MapExeIDToHashCode.Remove(exe.Id.ToString());
                        PacuInfusionMapCodeList.Remove(exe.Drugs.HashCode);
                    }
                    return;
                }
                //修改exe                
                if (oldHashCode.Equals(exe.Drugs.HashCode))
                {  //药配置不变
                    PatientDrugss.Remove(pDrug);
                    PatientDrugss.Add(exe);
                    PacuInfusionMapCodeList[oldHashCode] = PatientDrugss;
                }
                else
                {
                    //改变了Drug 配置
                    mapExeIDToHashCode[exe.Id.ToString()] = exe.Drugs.HashCode; //覆盖之前的
                    List<PatientDrugsModel> PatientDrugs = PacuInfusionMapCodeList[oldHashCode];
                    PatientDrugsModel pDrugs = PatientDrugs.Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
                    PatientDrugs.Remove(pDrugs);  //清除旧的
                    if (PatientDrugs.Count() < 1) PacuInfusionMapCodeList.Remove(oldHashCode);

                    List<PatientDrugsModel> PatientDrugsList = null;
                    if (!PacuInfusionMapCodeList.ContainsKey(exe.Drugs.HashCode))
                    {
                        PatientDrugsList = new List<PatientDrugsModel>();
                        PatientDrugsList.Add(exe);
                        PacuInfusionMapCodeList.Add(exe.Drugs.HashCode, PatientDrugsList);
                    }
                    else
                    {
                        PatientDrugsList = PacuInfusionMapCodeList[exe.Drugs.HashCode];
                        PatientDrugsList.Add(exe);
                        PacuInfusionMapCodeList[exe.Drugs.HashCode] = PatientDrugsList;
                    }
                }
            }
        }
        private void AddAnesSpecialDrug(PatientDrugsModel exe)
        {
            PatientDrugsModel pDrug = AnesSpecialDrugList.ToList().Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
            if ("invalid".Equals(exe.Status))
            {
                MapExeIDToHashCode.Remove(exe.Id.ToString());
                AnesSpecialDrugList.Remove(pDrug);
                return;
            }
            //yao  HashCode 改变        
            if (!mapExeIDToHashCode.ContainsKey(exe.Id.ToString()))
            {  //新增exe
                MapExeIDToHashCode.Add(exe.Id.ToString(), exe.Drugs.HashCode);
            }
            else
            {
                AnesSpecialDrugList.Remove(pDrug);
            }
            AnesSpecialDrugList.Add(exe);
        }

        private void AddPacuSpecialDrug(PatientDrugsModel exe)
        {
            PatientDrugsModel pDrug = PacuSpecialDrugList.ToList().Find(delegate (PatientDrugsModel p) { return p.Id == exe.Id; });//只返回第一个匹配
            if ("invalid".Equals(exe.Status))
            {
                MapExeIDToHashCode.Remove(exe.Id.ToString());
                PacuSpecialDrugList.Remove(pDrug);
                return;
            }
            //yao  HashCode 改变        
            if (!mapExeIDToHashCode.ContainsKey(exe.Id.ToString()))
            {  //新增exe
                MapExeIDToHashCode.Add(exe.Id.ToString(), exe.Drugs.HashCode);
            }
            else
            {
                PacuSpecialDrugList.Remove(pDrug);
            }
            PacuSpecialDrugList.Add(exe);
        }

        protected bool DbDelete(PatientDrugsModel data)
        {
            return PatientDrugsService.Instance.DelOneExe(data);
            //GetPatientDrugs8Time(pid, time1, time2);
        }
        protected bool DbSave(PatientDrugsModel data)
        {
            PatientDrugsService.Instance.addUpdate(data);
            return true;
        }
        public void AddAndUpdateExe(PatientDrugsModel PatientDrugs)
        {
            //List<PatientDrugsModel> plist = AllPatientDrugsList.ToList().FindAll(delegate (PatientDrugsModel p) { return p.Id == PatientDrugs.Id; });//返回所有匹配
            //bool b = AllPatientDrugsList.Any(p => p.Id == PatientDrugs.Id);         
            PatientDrugs.UpdateTime = DateTime.Now;
            DbSave(PatientDrugs);
            PatientDrugsModel pDrug = AllPatientDrugsList.ToList().Find(delegate (PatientDrugsModel p) { return p.Id == PatientDrugs.Id; });//只返回第一个匹配
            if (pDrug == null)
            {
                PatientDrugs.InputTime = DateTime.Now;
                AllPatientDrugsList.Add(PatientDrugs);
            }
            else
            {
                AllPatientDrugsList.Remove(pDrug);
                if (!"invalid".Equals(PatientDrugs.Status))
                {
                    AllPatientDrugsList.Add(PatientDrugs);
                }
            }
            addData(PatientDrugs);
        }

        public void DelOneExe(PatientDrugsModel PatientDrugs)
        {
            //indexDrugsCode.deleteData(PatientDrugs);
            DbDelete(PatientDrugs);  //db

        }

        #endregion

        #region EventHandlers

        #endregion

    }
}
